package com.lanxin.xxform.aspects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.lanxin.xxform.aspects.exceptions.ParamException;
import com.lanxin.xxform.aspects.exceptions.SessionException;

@Component
@Aspect
public class HomeAspect {

    /**
     * Dashboard页面会话切点
     */
    @Pointcut("execution(* com.lanxin.xxform.controllers.HomeController.*(..))")
    public void cutPoint() {}

    @Before("cutPoint()")
    public void checkSession(JoinPoint joinPoint) throws SessionException {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        if (ra == null) {
            throw new ParamException("前端请求参数错误");
        }
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
        HttpSession session = request.getSession();
        if (session.getAttribute("uid") == null) {
            throw new SessionException();
        }
    }
    
}
